package com.example.redislearn.example.sign.impl;

import com.example.redislearn.example.sign.SignService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RBitSet;
import org.redisson.api.RFuture;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class SignServiceImpl implements SignService {

    @Resource
    private Redisson redisson;

    @Override
    public boolean sign(String key, Integer value) {
        RBitSet bitSet = redisson.getBitSet(key);
        RFuture<Boolean> future = bitSet.setAsync(value);
        return future.isSuccess();
    }


    @Override
    public long getSignNum(String key) {
        RBitSet bitSet = redisson.getBitSet(key);
        return bitSet.cardinality();
    }


    @Override
    public boolean checkSign(String key, Integer userId) {
        RBitSet bitSet = redisson.getBitSet(key);
        return bitSet.get(userId);
    }
}
